﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Cache;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;
using Chinaer.Core.BaseCore.Extensions;

namespace Chinaer.Bll
{
    public class DianPu : Repository, Chinaer.IBll.IDianPu
    {
        Chinaer.IDao.IDianPuDAO dianPuDAO = new Chinaer.Dao.DianPuDAO();
        public DTO.DianPuDTO GetDianPu(string dianPuID)
        {
            if (CacheHelper.Memcached_KeyExists("DIANPU" + dianPuID))
            {
                return (DTO.DianPuDTO)CacheHelper.Memcached_Get(dianPuID);
            }

            Model.Entities.Model_DianPu model_DianPu = dianPuDAO.GetByID(dianPuID);
            if (model_DianPu == null)
            {
                return null;
            }
            Chinaer.DTO.DianPuDTO dianPuDTO = new DTO.DianPuDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_DianPu, dianPuDTO);
            CacheHelper.Memcached_Set("DIANPU" + dianPuID, dianPuDTO);

            return dianPuDTO;
        }

        public JsonResult Add(DTO.DianPuDTO t)
        {
            JsonResult jsonResult = new JsonResult();
            if (t.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull;
                jsonResult.ErrorMessage = jsonResult.LoginStatus.ToDescription();
                return jsonResult;
            }

            Model.Entities.Model_DianPu model_DianPu = new Model.Entities.Model_DianPu();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_DianPu);
            if (string.IsNullOrEmpty(model_DianPu.ID) || model_DianPu.ID.Trim() == "")
            {
                model_DianPu.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = dianPuDAO.Add(model_DianPu);
            jsonResult.ErrorMessage = model_DianPu.ID;
            return jsonResult;
        }

        public JsonResult Delete(DTO.DianPuDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_DianPu model_DianPu = new Model.Entities.Model_DianPu();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_DianPu);
            jsonResult.Success = dianPuDAO.Delete(model_DianPu);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = dianPuDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.DianPuDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_DianPu model_DianPu = new Model.Entities.Model_DianPu();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_DianPu);
            jsonResult.Success = dianPuDAO.Update(model_DianPu);
            return jsonResult;
        }


        public DTO.DianPuDTO GetDianPuByShangJiaID(string shangJiaID)
        {
            Model.Entities.Model_DianPu model_DianPu = dianPuDAO.GetDianPyByShangJiaID(shangJiaID);
            if (model_DianPu == null)
            {
                return null;
            }
            DTO.DianPuDTO dianPuDTO = new DTO.DianPuDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_DianPu, dianPuDTO);
            return dianPuDTO;
        }

        /// <summary>
        /// 根据编号获取店铺详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DTO.DianPuDTO GetByID(string id)
        {
            Model.Entities.Model_DianPu model_DianPu = dianPuDAO.GetByID(id);
            DTO.DianPuDTO dianPuDTO = new DTO.DianPuDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_DianPu, dianPuDTO);
            return dianPuDTO;
        }


        public List<DTO.DianPuDTO> GetAllDianPus()
        {
            List<DTO.DianPuDTO> listDianPuDTO = new List<DTO.DianPuDTO>();
            try
            {
                List<Model.Entities.Model_DianPu> listModelDianPu = new List<Model.Entities.Model_DianPu>();
                listModelDianPu = dianPuDAO.GetAllDianPus();
                foreach (var item in listModelDianPu)
                {
                    DTO.DianPuDTO dianPuDTO = new DTO.DianPuDTO();
                    Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, dianPuDTO);
                    listDianPuDTO.Add(dianPuDTO);
                }
            }
            catch (Exception ex)
            {
                listDianPuDTO = null;
            }
            return listDianPuDTO;
        }
    }
}
